let http=require('http');


module.exports=function(path){
  //声明一个期望对象
  let promise=new Promise(function(resolve,reject){

    http.get(path, (res) => {
  const { statusCode } = res;
  const contentType = res.headers['content-type'];

  let error;
  if (statusCode !== 200) {
    error = new Error('请求失败。\n' +
                      `状态码: ${statusCode}`);
  } 

  if (error) {
    reject(error.message);
    // 消耗响应数据以释放内存
    res.resume();
    return;
  }

  res.setEncoding('utf8');
  let rawData = '';
  res.on('data', (chunk) => { rawData += chunk; });
  res.on('end', () => {
    try {
      resolve(rawData);
    } catch (e) {
      reject(e.message);
    }
  });
}).on('error', (e) => {
  reject(`错误: ${e.message}`);
});

  });
  return promise;//返回这个声明对象
}